<?php
/**
 * 微信支付
 */
namespace LancerWechat\Pay;

use LancerWechat\WechatService;

class WechatPay extends WechatService
{
    private $key = 'pay';
    private $orderParam;
    /**
     * 统一下单输入对象
     * @var WechatPayUnifiedOrder
     */
    private $WechatPayUnifiedOrder;
    /**
     * 提交被扫输入对象
     * @var WechatPayJSPayApi
     */
    private $WechatPayJSPayApi;
    /**
     * 订单类
     * @var WechatPayOrderQuery
     */
    public $order;
    /**
     * 回调基础类
     */
    public $notify;

    /**
     * 初始化加载
     */
    public function load()
    {
        $this->WechatPayUnifiedOrder = new  WechatPayUnifiedOrder();
        $this->WechatPayJSPayApi = new WechatPayJSPayApi();
        $this->notify = new WechatPayNotifyCallback();
        $this->notify->setConfig($this->config);
        $this->order = new WechatPayOrderQuery();
    }


    /**
     * 创建订单
     * @param array $param
     */
    public function create(array $param)
    {

        // 分配数据
        if (isset($param['body'])) {
            $this->WechatPayUnifiedOrder->SetBody($param['body']);
        }
        if (isset($param['attach'])) {
            $this->WechatPayUnifiedOrder->SetAttach($param['attach']);
        }
        if (isset($param['out_trade_no'])) {
            $this->WechatPayUnifiedOrder->SetOut_trade_no($param['out_trade_no']);
        }
        if (isset($param['total_fee'])) {
            $this->WechatPayUnifiedOrder->SetTotal_fee($param['total_fee']);
        }
        if (isset($param['notify_url'])) {
            $this->WechatPayUnifiedOrder->SetNotify_url($param['notify_url']);
        }
        if (isset($param['time_start'])) {
            $this->WechatPayUnifiedOrder->SetTime_start($param['notify_url']);
        } else {
            $this->WechatPayUnifiedOrder->SetTime_start(date("YmdHis"));
        }
        if (isset($param['time_expire'])) {
            $this->WechatPayUnifiedOrder->SetTime_expire($param['notify_url']);
        } else {
            $this->WechatPayUnifiedOrder->SetTime_expire(date("YmdHis", time() + 600));
        }
        if (isset($param['goods_tag'])) {
            $this->WechatPayUnifiedOrder->SetGoods_tag($param['goods_tag']);
        }
        if (isset($param['trade_type'])) {
            $this->WechatPayUnifiedOrder->SetTrade_type($param['trade_type']);
        }
        if (isset($param['openid'])) {
            $this->WechatPayUnifiedOrder->SetOpenid($param['openid']);
        }
    }

    /**
     * 统一下单
     */
    public function unifiedOrder()
    {
        $this->orderParam = WechatPayApi::unifiedOrder($this->config, $this->WechatPayUnifiedOrder, 6);
    }

    /**
     * 获取订单信息
     * @param string $transaction_id
     */
    public function getOrder(string $transaction_id)
    {
        $this->order->SetTransaction_id($transaction_id);
        return WechatPayApi::orderQuery($this->config, $this->order);
    }

    /**
     *
     * 获取jsapi支付的参数
     * @param array $UnifiedOrderResult 统一支付接口返回的数据
     * @throws WxPayException
     *
     * @return json数据，可直接填入js函数作为参数
     */
    public function getJsApiParameters()
    {
        $this->WechatPayJSPayApi->setConfig($this->config);
        return $this->WechatPayJSPayApi->GetJsApiParameters($this->orderParam);
    }

    /**
     *
     * 获取jsapi支付的参数
     * @param array $UnifiedOrderResult 统一支付接口返回的数据
     * @throws WxPayException
     *
     * @return json数据，可直接填入js函数作为参数
     */
    public function getEditAddressParameters()
    {
        $this->WechatPayJSPayApi->setConfig($this->config);
        return $this->WechatPayJSPayApi->GetEditAddressParameters();
    }
}